package com.digitalart.site.main {
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	import com.digitalart.site.section.SectionFactory;
	import com.digitalart.site.commons.CustomEvent;
	import com.digitalart.site.section.SectionContainer;
	import com.digitalart.site.assets.backAnimation;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;

	import com.digitalart.site.menu.MenuController;
	import flash.display.Sprite;

	/**
	 * @author johan.chaves
	 */
	 [SWF (backgroundColor='#996600',  frameRate='40', width='980', height='593') ]
	 
	public class MainApp extends Sprite {
		
		public static const SECTION_CONTAINER_X : Number = 195;
		public static const SECTION_CONTAINER_Y : Number = 70;
		
		private var _menuController : MenuController;
		private var _sectionContainer : SectionContainer;
		
		private var _sectionFactory : SectionFactory;
		
		public function MainApp() {
			
			stage.scaleMode=StageScaleMode.NO_SCALE;
			stage.align=StageAlign.TOP_LEFT;						
			
			addBackgroundAnimation();
			addSectionContainer();
			
			_sectionFactory = SectionFactory.getInstance();
			
			_menuController = new MenuController();
			_menuController.addEventListener(MenuController.ON_MENU_READY, onMenuReady);
			_menuController.addEventListener(MenuController.MENU_ITEM_CLICKED, onMenuItemClicked);
			_menuController.init();
		}
		
		private function onMenuItemClicked(event : CustomEvent) : void {
			
			_menuController.disableItems();
			
			_sectionContainer.removeCloseEvents();
			
			_sectionContainer.removeSection();
			
			_sectionFactory.addEventListener(SectionFactory.ON_SECTION_READY, addSection);																	
			_sectionFactory.prepareSection(event.data[0], event.data[1]);
			
			_sectionContainer.startInAnimation();
		}
		
		private function addSection(event : Event) : void {
			
			_sectionFactory.removeEventListener(SectionFactory.ON_SECTION_READY, addSection);
			
			var timer:Timer = new Timer(1000, 3);
			
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, testLoader);
			
			timer.start();
			
//			_sectionContainer.removeLoader();
//			_sectionContainer.addSection(_sectionFactory.section);
//			_menuController.enableItems();
//			_sectionContainer.addCloseEvents();
//			_sectionFactory.unloadSection();
		}

		private function testLoader(e:TimerEvent) : void {
			_sectionContainer.removeLoader();
			_sectionContainer.addSection(_sectionFactory.section);
			_menuController.enableItems();
			_sectionContainer.addCloseEvents();
			_sectionFactory.unloadSection();
		}
		
		private function addBackgroundAnimation() : void {
			
			var anim : backAnimation = new backAnimation();			
			addChild(anim);
		}
		
		private function addSectionContainer() : void {			
			
			_sectionContainer = new SectionContainer();
			
			_sectionContainer.x = SECTION_CONTAINER_X;
			_sectionContainer.y = SECTION_CONTAINER_Y;
			
			_sectionContainer.addEventListener(SectionContainer.ON_CONTAINER_OUT, onContainerOut);
			_sectionContainer.addEventListener(SectionContainer.ON_ANIMATION_IN_COMPLETE, onContainerIn);
			
			addChild(_sectionContainer);		
		}
		
		private function onContainerIn(event : Event) : void {
			
			_sectionContainer.addLoader();
			_sectionFactory.createSection();
		}

		private function onContainerOut(event : Event) : void {
			
			_menuController.enableItems();
			_menuController.unSelectAllItems();
		}

		private function onMenuReady(event : Event) : void {	
			
			_menuController.removeEventListener(MenuController.ON_MENU_READY, onMenuReady);	
				
			this.addChild(_menuController.getHorizontalMenu());
			this.addChild(_menuController.getVerticalMenu());
		}
	}
}
